package b2.widget.store
{
	import b2.config.*;
	import b2.net.*;
	import b2.player.*;
	import b2.scene.*;
	import b2.util.*;
	import b2.widget.*;
	import b2.widget.tab.*;
	
	import flash.display.Sprite;
	import flash.events.*;
	import flash.filters.*;
	import flash.geom.Point;
	import flash.text.*;
	import flash.ui.Mouse;

	public class StoreItem extends Sprite
	{
		private var objectIndex:int;
		//背景
		private var tabItemBackground:Sprite;
		//能否购买标识
		private var storeItemLock:Sprite;

		public function StoreItem(objectIndex:int)
		{
			super();
			this.objectIndex = objectIndex; 
			var objectConfig:CommonObjConfig = Config.objectConfigs[objectIndex];
			//背景框
			var StoreItemBgr:Class = ResourceHelper.getInstance().getClassByName("StoreItemBgr");
			tabItemBackground = new StoreItemBgr();
			addChild(tabItemBackground);
			//标题名字
			var titleText:TextField = new TextField();
			titleText.selectable = false;
        	titleText.autoSize = TextFieldAutoSize.LEFT;
			//装备
			if (objectConfig.type == 1)
			{
				var equipConfig:EquipConfig = objectConfig as EquipConfig;
				titleText.text = Config.basicConfig.equipSuitName[equipConfig.suitIndex] + "的";
			}
			titleText.appendText(objectConfig.name);
			if (titleText.text.length > 6)
			{
				titleText.setTextFormat(new TextFormat("微软雅黑", 12, 0x000000, true));
				titleText.y = 9;
			}
			else
			{
				titleText.setTextFormat(new TextFormat("微软雅黑", 15, 0x000000, true));
				titleText.y = 7;
			}
			titleText.x = (this.width - titleText.width) / 2;
			if (!objectConfig.noBasicLevel)
			{
				//背景框
				var ObjectItemBgr:Class = ResourceHelper.getInstance().getClassByName("ObjectItemBgr0");
				var objectItemBgr:Sprite = new ObjectItemBgr();
				objectItemBgr.width = 69;
				objectItemBgr.height = 69;
				objectItemBgr.x = 17.5;
				objectItemBgr.y = 27.5;
				addChild(objectItemBgr);
			}
			//物品图片
			var ObjectImage:Class = ResourceHelper.getInstance().getClassByName(objectConfig.iconImage);
			var objectSprite:Sprite = new ObjectImage();
			//齐天大圣 
			if (objectIndex == 10015)
			{
				objectSprite.width = 89;
				objectSprite.height = 100;
			}
			//死亡骑士
			else if (objectIndex == 10020)
			{
				objectSprite.width = 78;
				objectSprite.height = 78;
			}
			else
			{
				objectSprite.width = 65;
				objectSprite.height = 65;
			}
			objectSprite.x = 19.5;
			objectSprite.y = 29.5;
			addChild(objectSprite);
			addChild(titleText);
			
			//非卖品
			if (!objectConfig.onSaleFlag)
			{
				var OnSaleLock:Class = ResourceHelper.getInstance().getClassByName("StoreItemLock2");
				var onSaleLock:Sprite = new OnSaleLock();
				onSaleLock.x = 12;
				onSaleLock.y = 60;
				addChild(onSaleLock);
			}
			else
			{
				//加载价格图标和价格
				var priceContainer:Sprite = new Sprite();
				//价格图标
				var PriceImage:Class;
				var price:int;
				if (objectConfig.priceGas > 0)
				{
					PriceImage = ResourceHelper.getInstance().getClassByName("PriceGas");
					price = objectConfig.priceGas;
				}
				else if (objectConfig.priceHonour > 0)
				{
					PriceImage = ResourceHelper.getInstance().getClassByName("Honour");
					price = objectConfig.priceHonour;
				}
				else
				{
					PriceImage = ResourceHelper.getInstance().getClassByName("PriceOil");
					price = objectConfig.priceOil;
				}
				
				var priceImage:Sprite = new PriceImage();
				priceImage.width = 20;
				priceImage.height = 20;
				priceContainer.addChild(priceImage);
				//价格
				var priceText:TextField = new TextField();
				priceText.selectable = false;
				priceText.autoSize = TextFieldAutoSize.LEFT;
				priceText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0x000000);
				priceText.text = price.toString();
				priceText.x = priceImage.x + priceImage.width + 5;
				priceText.y = priceImage.y - 2;
				priceContainer.addChild(priceText);
				
				priceContainer.x = (tabItemBackground.width - priceContainer.width) / 2;
				priceContainer.y = 100;
				this.addChild(priceContainer);
				
			}
			
			//锁
			var StoreItemLock:Class = ResourceHelper.getInstance().getClassByName("StoreItemLock1");
			storeItemLock = new StoreItemLock();
			storeItemLock.x = 22;
			storeItemLock.y = 65;
			addChild(storeItemLock);
			
			refresh();
			addEventListener(MouseEvent.CLICK, onMouseClick);
			addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
		}
		
		private function onMouseClick(event:MouseEvent):void
		{
			var me:PlayerInfo = PlayerInfoManager.getInstance().me;
			var objectConfig:CommonObjConfig = Config.objectConfigs[objectIndex];
			if (!objectConfig.onSaleFlag)
			{
	        	var popMessage:PopMessage = new PopMessage("非卖品，只在战役和任务中获得！", 0xFF0000);
	        	WidgetManager.getInstance().popOut([popMessage], false, true);
			}
			else if (me.level < objectConfig.requireLevel)
			{
	        	popMessage = new PopMessage("等级不足！需要等级：" + objectConfig.requireLevel, 0xFF0000);
	        	WidgetManager.getInstance().popOut([popMessage], false, true);
			}
			else
			{
				var purchaseDialog:PurchaseDialog = new PurchaseDialog(0, objectIndex, 0, 0, 0);
				WidgetManager.getInstance().popOut([purchaseDialog], false, true);
			}
		}

		private function onMouseOver(event:MouseEvent):void
		{
			Mouse.cursor = "button";
			this.filters = [new ColorMatrixFilter(BFont.brightFilter)];
			var me:PlayerInfo = PlayerInfoManager.getInstance().players[PlayerInfoManager.getInstance().myUserId];
			var objectConfig:CommonObjConfig = Config.objectConfigs[objectIndex];
			var globalPoint:Point = this.parent.localToGlobal(new Point(this.x, this.y));
			var itemTip:StoreItemMessage = new StoreItemMessage(objectConfig.index); 
			itemTip.x = globalPoint.x + this.width;
			itemTip.y = globalPoint.y;
			if (itemTip.x + itemTip.width > BFont.stageWidth)
				itemTip.x = globalPoint.x - itemTip.width;
			if (itemTip.y + itemTip.height > BFont.stageHeight)
				itemTip.y = BFont.stageHeight - itemTip.height;
			WidgetManager.getInstance().popOutMouseOverMessage(itemTip);
		}
		
		private function onMouseOut(event:MouseEvent):void
		{
			Mouse.cursor = "auto";
			this.filters = null;
			WidgetManager.getInstance().closeMouseOverMessage();
		}
		
		//刷新，能够买、不能够买的分开
		public function refresh():void
		{
			var me:PlayerInfo = PlayerInfoManager.getInstance().me;
			var objectConfig:CommonObjConfig = Config.objectConfigs[objectIndex];
			if (objectConfig.onSaleFlag && me.level < objectConfig.requireLevel)
			{
				storeItemLock.visible = true;
			}
			else
			{
				storeItemLock.visible = false;
			}
		}
		
	}
}